package com.XinYun.Library.modules.person;

import com.XinYun.Library.modules.commons.BasicServlet;
import com.XinYun.Library.modules.person.Dao.SQLDao;
import com.XinYun.Library.modules.person.Response.ResponseWrapper;
import com.XinYun.Library.utils.HTTPRequestUtils.HTTPRequestUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "ViewServlet", value = "/ViewServlet")
public class ViewServlet extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String json = HTTPRequestUtil.getBody(req);
        //将JSON字符转化为JAVA对象
        System.out.println(req.getContextPath());
        JSONObject jsonObject = JSONObject.parseObject(json);
        String value = jsonObject.getString("uid");
        if(value==""){
            jsonObject.put("code",-1);
            jsonObject.put("message","uid为空");
        }
        SQLDao services = new SQLDao();
        services.getuser(value);
        if(services.getuser(value)==null){
            ResponseWrapper responseWrapper = ResponseWrapper.error();
            String s=JSON.toJSONString(responseWrapper);
            PrintWriter writer = resp.getWriter();
            writer.println(s);
            System.out.println(s);
            jsonObject.put("code",-1);
            jsonObject.put("message","返回信息失败");
        }else {
            String s = JSON.toJSONString(services.getuser(value));
            PrintWriter writer = resp.getWriter();
            jsonObject.put("code",1);
            jsonObject.put("message","返回信息成功");
            writer.println(s);
            resp.getWriter().write(jsonObject.toString());
        }
    }
}




